package Dao;

import java.io.IOException;
import java.io.InputStream;
import java.sql.Date;
import java.util.ArrayList;
import java.util.List;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;


import Entity.Room_ced;



import mapper.Schedulemapper;
import mapper.Selectionmapper;



public class scheduledao implements Schedulemapper {
	private static Schedulemapper schedulemapper;
	private static SqlSession session;

	public scheduledao() throws IOException {
	 	String resource ="mybatis-config.xml";
		InputStream inputstream = Resources.getResourceAsStream(resource);
		SqlSessionFactory sqlsessionfactory = new SqlSessionFactoryBuilder().build(inputstream);
		 session = sqlsessionfactory.openSession();
        schedulemapper = session.getMapper(Schedulemapper.class);
    }
	public void close(){
		this.session.close();
	}
	
	//鏌ヨ鏌愭棩鐨勬帓璇�
	public List<Room_ced> Room_ced_QueryByDate(Date date) {
		
		List<Room_ced> selectList = this.Room_ced_QueryByDate(date);
		return selectList;
	}
	
	//鍒涘缓鎺掕
	public boolean CreatSchedule(Room_ced room_ced) {
		rankdao rm = null;
		try {
			rm = new rankdao();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		int course_Id = rm.getNum("course_Id");
		room_ced.setCourse_Id(String.valueOf(course_Id));
		room_ced.setSchedule_Id(String.valueOf(course_Id));
		boolean iuser = this.schedulemapper.CreatSchedule(room_ced);
		if(iuser == true){
			session.commit();
		}else{
			session.rollback();
		}

		return iuser;
	}
	
	@Override
	//鏍规嵁id鍜屾棩鏈熷緱鍒版墍鏈夐�夎淇℃伅
	public List<Room_ced> getAllSchedule(String course_Id,Date date) {
		
		List<Room_ced> i = this.schedulemapper.getAllSchedule(course_Id,date);
		
		return i;
	}

	@Override
	public void deleteSchedule(String cid) {
	
		this.schedulemapper.deleteSchedule(cid);
		session.commit();

		
	}

	@Override
	public Room_ced getSchedule(String cid) {
		
		Room_ced selectList = this.schedulemapper.getSchedule(cid);
		
		return selectList;
	}
	
	public boolean querySchedule(String rid, Date date, int order) {
		
		boolean iuser = this.schedulemapper.querySchedule(rid,date,order);
		
		return iuser;
	}

	@Override
	public String querySchedule1(String rid, Date date, int order) {
		String iuser = this.schedulemapper.querySchedule1(rid,date,order);
		return iuser;
	}


	@Override
	public List<Room_ced> queryScheduleBycid(String id) {
		Selectionmapper sm = null;
		try {
			sm = new selectiondao();
		} catch (IOException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		List<String> sl = sm.querySelection(id);
		Schedulemapper ssm = null;
		try {
			ssm = new scheduledao();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			
		}
		List<Room_ced> ssl = new ArrayList<Room_ced>();
		System.out.println(ssl);
		for(int i=0;i<sl.size();i++){
			if(ssm.getSchedule(sl.get(i))!=null)
				ssl.add(ssm.getSchedule(sl.get(i)));
			
		}
		
		return ssl;
	}


}

 
